<?php
namespace app\admin\model;
use think\Model;
use think\Db;

class KnowsModel extends Model{

    /*
     * 育儿知识列表查询
     */
    public function selectAll($data)
    {
        $is_home = isset($data['is_home'])?$data['is_home']:0;
        $is_vouch = isset($data['is_vouch'])?$data['is_vouch']:0;
        $is_top = isset($data['is_top'])?$data['is_top']:0;
        $parenting_title = isset($data['parenting_title'])?$data['parenting_title']:'';

        if($is_home != 0 || $is_vouch != 0 ||$is_top != 0 || $parenting_title != ''){
            $rows = Db::table('knows')
                ->alias('a')
                ->join('knows_type b', 'a.knows_type_id=b.id')
                ->where("a.is_del=0 and b.is_del=0 and a.is_home = $is_home and a.is_vouch = $is_vouch and a.is_top = $is_top ")
                ->where('parenting_title','like',"$parenting_title%")
                ->order('a.knows_time desc')
                ->paginate(10);
            return $rows;
        }else{
            $res = Db::table('knows')
                ->alias('a')
                ->field('a.id,a.parenting_title,a.parenting_content,a.album_url,a.knows_time,a.is_home,a.is_vouch,a.is_top,b.know_name')
                ->join('knows_type b', 'a.knows_type_id=b.id')
                ->where('a.is_del=0','b.is_del=0')
                ->order('a.knows_time desc')
                ->paginate(10);
            return $res;
        }
    }
    /*
     *育儿知识查询
     */
    public function select()
    {
        $res = Db::table('knows_type')->where('parent_id = 0','is_del=0')->select();
        $res =$this->showTree($res);
        return $res;
    }
    /*
     *查询育儿分类id
     */
    public function getKnowsId($data)
    {
        $res = Db::table('knows_type')->where($data)->select();
        return $res;
    }

    //无限极分类
    public function showTree($arr, $html = "&nbsp;&nbsp", $pid = 0, $level = 0){
        static $newArr = array();
        foreach($arr as $key => $v){
            if($v['parent_id'] == $pid) {
                $v['level'] = $level + 1;
                $html2 = ($level == 0) ? '' : '▷';//生成目录▷
                $v['html'] = str_repeat($html, $level) . $html2;
                $v['know_name'] = $v['html'] . $v['know_name'];
                $newArr[] = $v;

                $this->showTree($arr, $html , $v['id'], $v['level']);
            }
        }
        return $newArr;
    }
    /*
     *添加
     */
    public function insertRow($data)
    {
        $row = Db::table('knows')->insert($data);
        return $row;
    }
    /*
     *查询一条
     */
    public function selectFind($id)
    {
        $res = Db::table('knows')->where(array('id'=>$id))->find();
        return $res;
    }
    /*
     *修改
     */
    public function saveFind($data){
        $row = Db::table('knows')->update($data);
        return $row;
    }

    /*
     *删除一条
     */
    public function deleteFind($id){
        $is_del['is_del'] = 1;
        $res = Db::table('knows')->where("id=$id")->update($is_del);
        return $res;
    }


    /*
     *删除多条
     */
    public function deleteAll($id)
    {
        $res = Db::table('knows')->where('id', $id)->update(['is_del' => 1]);
        return $res;
    }


    // 首页设置

    public function getHome($id){
        $res = Db::table('knows')->where('id',$id)->update(['is_home'=>1]);
        return $res;
    }
    // 取消首页
    public function getCancelHome($id){
        $res = Db::table('knows')->where('id',$id)->update(['is_home'=>0]);
        return $res;
    }

    // 推荐设置
    public function getRecom($id){
        $res = Db::table('knows')->where('id',$id)->update(['is_vouch'=>1]);
        return $res;
    }
    // 取消推荐
    public function getcancelRecom($id){
        $res = Db::table('knows')->where('id',$id)->update(['is_vouch'=>0]);
        return $res;
    }
    //置顶设置
    public function getTop($id){
        $res = Db::table('knows')->where('id',$id)->update(['is_top'=>1]);
        return $res;
    }
    // 取消置顶
    public function getCancelTop($id){
        $res = Db::table('knows')->where('id',$id)->update(['is_top'=>0]);
        return $res;
    }

}